package com.example.myanimator.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.myanimator.R;

/**
 * @author tgw
 * @date 2021/3/18
 * @describe
 */
class BoundView extends FrameLayout {

    private int radius = 800;
    private CircleImageView circleImageView, circleImageView1, circleImageView2, circleImageView3, circleImageView4, circleImageView5;

    private AnimatorSet animatorSet;
    private ObjectAnimator animator1,animator2,animator3,animator4,animator5;

    public BoundView(@NonNull Context context) {
        this(context, null);
    }

    public BoundView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BoundView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.bound_view_layout, this);
        initView();
    }

    private void initView() {
        circleImageView = findViewById(R.id.leke_group_space_civ_header);
        circleImageView1 = findViewById(R.id.leke_group_space_civ_header1);
        circleImageView2 = findViewById(R.id.leke_group_space_civ_header2);
        circleImageView3 = findViewById(R.id.leke_group_space_civ_header3);
        circleImageView4 = findViewById(R.id.leke_group_space_civ_header4);
        circleImageView5 = findViewById(R.id.leke_group_space_civ_header5);


        circleImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                doAnimator(circleImageView1,1);
                doAnimator(circleImageView2,2);
                doAnimator(circleImageView3,3);
                doAnimator(circleImageView4,4);
                doAnimator(circleImageView5,5);
            }
        });
    }

    private void doAnimator(CircleImageView circleImageView1,int index) {
        float translationX = (float)(-radius*Math.sin(Math.toRadians(90/4*index)));
        float translationY = (float)(-radius*Math.cos(Math.toRadians(90/4*index)));
        animator1 = ObjectAnimator.ofFloat(circleImageView1,"translationX",circleImageView.getX(),translationX);
        animator1.setInterpolator(new AccelerateInterpolator());
//        animator1.setDuration(3000);
        animatorSet = new AnimatorSet();
//        animatorSet.setDuration(3000);
        animatorSet.playTogether(animator1,ObjectAnimator.ofFloat(circleImageView1,"translationY",circleImageView.getY(),translationY),ObjectAnimator.ofFloat(circleImageView1,"scaleX",0,1),ObjectAnimator.ofFloat(circleImageView1,"scaleY",0,1),ObjectAnimator.ofFloat(circleImageView1,"alpha",0,1));
        animatorSet.start();
    }


}
